<?php
namespace Home\Controller;
use Lib\Api\DeliveryAddressApi;
use Think\Controller;
class ShoppingController extends Controller {

    public function index() {
        $this->display();
    }

    public function category($type) {

        $SHOPPING = C('TMPL_PARSE_STRING.__SHOPPING__');
        $DOMAINURL = C('TMPL_PARSE_STRING.__DOMAINURL__');
        $WEBSITEDIR = C('TMPL_PARSE_STRING.__WEBSITEDIR__');

        $url = $DOMAINURL.$WEBSITEDIR.$SHOPPING;

        $banner = "";
        $list = array();
            if ($type == 1) {
            $banner = $url."/category/image/image-1.png";
            $category["id"] = 1;
            $category["img"] = $url."/image/icon-v1.jpg";
            $category["name"] = "【碧玉】葫芦";
            $category["money"] = 1599;
            array_push($list, $category);

            $category2["id"] = 4;
            $category2["img"] = $url."/image/icon-v4.jpg";
            $category2["name"] = "【碧玉】手镯";
            $category2["money"] = 2828;
            array_push($list, $category2);

            $category3["id"] = 7;
            $category3["img"] = $url."/image/icon-v7.jpg";
            $category3["name"] = "【碧玉】平安扣";
            $category3["money"] = 666;
            array_push($list, $category3);

        } else if ($type == 2) {
            $banner = $url."/category/image/image-2.png";
            $category["id"] = 2;
            $category["img"] = $url."/image/icon-v2.jpg";
            $category["name"] = "【蓝碧玺】吊坠";
            $category["money"] = 6600;
            array_push($list, $category);

            $category2["id"] = 5;
            $category2["img"] = $url."/image/icon-v5.jpg";
            $category2["name"] = "【红碧玺】戒指";
            $category2["money"] = 5599;
            array_push($list, $category2);

            $category3["id"] = 8;
            $category3["img"] = $url."/image/icon-v8.jpg";
            $category3["name"] = "【红碧玺】手镯";
            $category3["money"] = 8888;
            array_push($list, $category3);
        } else if ($type == 3) {
            $banner = $url."/category/image/image-3.png";
            $category["id"] = 3;
            $category["img"] = $url."/image/icon-v3.jpg";
            $category["name"] = "【紫檀】手串";
            $category["money"] = 2626;
            array_push($list, $category);

            $category2["id"] = 6;
            $category2["img"] = $url."/image/icon-v6.jpg";
            $category2["name"] = "【紫檀】葫芦";
            $category2["money"] = 3609;
            array_push($list, $category2);

            $category3["id"] = 9;
            $category3["img"] = $url."/image/icon-v9.jpg";
            $category3["name"] = "【紫檀】佛珠";
            $category3["money"] = 5210;
            array_push($list, $category3);
        }
        $this->assign('banner', $banner);
        $this->assign('list', $list);
        $this->display();
    }

    public function details($id) {
        $SHOPPING = C('TMPL_PARSE_STRING.__SHOPPING__');
        $DOMAINURL = C('TMPL_PARSE_STRING.__DOMAINURL__');
        $WEBSITEDIR = C('TMPL_PARSE_STRING.__WEBSITEDIR__');

        $url = $DOMAINURL.$WEBSITEDIR.$SHOPPING;

        $category = array();
      if ($id == 1) {
            $category["id"] = 1;
            $category["banner"] = $url."/image/icon-v1.jpg";
            $category["detail_banner"] = $url."/image/icon-b1.jpg";
            $category["img"] = $url."/image/detail-v1.jpg";
            $category["name"] = "【碧玉】和田葫芦";
            $category["money"] = 1599;
        } else if ($id == 2) {
            $category["id"] = 2;
            $category["banner"] = $url."/image/icon-v2.jpg";
            $category["detail_banner"] = $url."/image/icon-b2.jpg";
            $category["img"] = $url."/image/detail-v2.jpg";
            $category["name"] = "【蓝碧玺】经典吊坠";
            $category["money"] = 6601;
        } else if ($id == 3) {
            $category["id"] = 3;
            $category["banner"] = $url."/image/icon-v3.jpg";
            $category["detail_banner"] = $url."/image/icon-b3.jpg";
            $category["img"] = $url."/image/detail-v3.jpg";
            $category["name"] = "【紫檀】野生檀手串";
            $category["money"] = 2626;
        } else if ($id == 4) {
            $category["id"] = 4;
            $category["banner"] = $url."/image/icon-v4.jpg";
            $category["detail_banner"] = $url."/image/icon-b4.jpg";
            $category["img"] = $url."/image/detail-v4.jpg";
            $category["name"] = "【碧玉】和田手镯";
            $category["money"] = 2828;
        } else if ($id == 5) {
            $category["id"] = 5;
            $category["banner"] = $url."/image/icon-v5.jpg";
            $category["detail_banner"] = $url."/image/icon-b5.jpg";
            $category["img"] = $url."/image/detail-v5.jpg";
            $category["name"] = "【红碧玺】鸽血红戒指";
            $category["money"] = 5599;
        } else if ($id == 6) {
            $category["id"] = 6;
            $category["banner"] = $url."/image/icon-v6.jpg";
            $category["detail_banner"] = $url."/image/icon-b6.jpg";
            $category["img"] = $url."/image/detail-v6.jpg";
            $category["name"] = "【紫檀】老料檀葫芦";
            $category["money"] = 3609;
        } else if ($id == 7) {
            $category["id"] = 7;
            $category["banner"] = $url."/image/icon-v7.jpg";
            $category["detail_banner"] = $url."/image/icon-b7.jpg";
            $category["img"] = $url."/image/detail-v7.jpg";
            $category["name"] = "【碧玉】和田平安扣";
            $category["money"] = 666;
        } else if ($id == 8) {
            $category["id"] = 8;
            $category["banner"] = $url."/image/icon-v8.jpg";
            $category["detail_banner"] = $url."/image/icon-b8.jpg";
            $category["img"] = $url."/image/detail-v8.jpg";
            $category["name"] = "【红碧玺】维纳斯手镯";
            $category["money"] = 8888;
        } else if ($id == 9) {
            $category["id"] = 9;
            $category["banner"] = $url."/image/icon-v9.jpg";
            $category["detail_banner"] = $url."/image/icon-b9.jpg";
            $category["img"] = $url."/image/detail-v9.jpg";
            $category["name"] = "【紫檀】小叶檀佛珠";
            $category["money"] = 5210;
        }

        $this->assign('category', $category);
        $this->display();
    }

    public function extractlog() {
        if (empty(session('user_id'))) {
            session("showLogin", 1);
            $rel_url = "../index";
            redirect($rel_url);
            // $this->display($rel_url);
            exit;
        }
        $this->display();
    }

    public function extract() {
        if (empty(session('user_id'))) {
            session("showLogin", 1);
            $rel_url = "../index";
            redirect($rel_url);
            // $this->display($rel_url);
            exit;
        }

        $this->display();
    }

    public function buy($id) {
        $SHOPPING = C('TMPL_PARSE_STRING.__SHOPPING__');
        $DOMAINURL = C('TMPL_PARSE_STRING.__DOMAINURL__');
        $WEBSITEDIR = C('TMPL_PARSE_STRING.__WEBSITEDIR__');

        $url = $DOMAINURL.$WEBSITEDIR.$SHOPPING;

        if (empty(session('user_id'))) {
            session("showLogin", 1);
            $rel_url = "../index";
            redirect($rel_url);
            // $this->display($rel_url);
            exit;
        }

        $category = array();
        if ($id == 1) {
            $category["id"] = 1;
            $category["img"] = $url."/image/icon-v1.jpg";
            $category["name"] = "【碧玉】葫芦";
            $category["money"] = 1600;
        } else if ($id == 2) {
            $category["id"] = 2;
            $category["img"] = $url."/image/icon-v2.jpg";
            $category["name"] = "【蓝碧玺】吊坠";
            $category["money"] = 6600;
        } else if ($id == 3) {
            $category["id"] = 3;
            $category["img"] = $url."/image/icon-v3.jpg";
            $category["name"] = "【紫檀】手串";
            $category["money"] = 2600;
        }

        $user_id = session("user_id");
        $deliveryAddressList = DeliveryAddressApi::getList(array("user_id"=>$user_id));
        $deliveryAddress['id'] = 0;
        $deliveryAddress['rel_name'] = "";
        $deliveryAddress['mobile'] = "";
        $deliveryAddress['province'] = "";
        $deliveryAddress['city'] = "";
        $deliveryAddress['address'] = "";
        if (!empty($deliveryAddressList)) {
            $deliveryAddress = $deliveryAddressList[0];
        }
        $this->assign('deliveryAddress', json_encode($deliveryAddress));
        $this->assign('category', $category);
        $this->display();
    }

    public function saveinfo() {
        if (empty(session('user_id'))) {
            session("showLogin", 1);
            $rel_url = "../index";
            redirect($rel_url);
            // $this->display($rel_url);
            exit;
        }
        $modelObj = M("DeliveryAddress");
        $data = $modelObj->create($_POST);
        if (empty($data['rel_name'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '收货人真实姓名不能为空';
            $this->ajaxReturn($ajaxdata);
        }
        if (empty($data['mobile'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '请填写手机号码';
            $this->ajaxReturn($ajaxdata);
        }
        if (empty($data['province'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '请填写省份';
            $this->ajaxReturn($ajaxdata);
        }
        if (empty($data['city'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '请填写市';
            $this->ajaxReturn($ajaxdata);
        }
        if (empty($data['address'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '请填写详细地址';
            $this->ajaxReturn($ajaxdata);
        }

        if ($data['id'] == 0) {
            $data['user_id'] = session('user_id');
            $id = DeliveryAddressApi::add($data);
        } else {
            DeliveryAddressApi::save($data);
            $id = $data['id'];
        }
        $ajaxdata['id'] = $id;
        $ajaxdata['code'] = 1;
        $this->ajaxReturn($ajaxdata);
    }

    public function sub_buy() {
        $ajaxdata['msg'] = '商品已下架';
        $this->ajaxReturn($ajaxdata);
    }
}